;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.LogisticsInformationReport", [])
        .controller("LogisticsInformationReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "EventBus", "Upload", "ybConfig", "UserContextService","$http", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, EventBus, Upload, ybConfig, UserContextService, $http) {
            $scope.activeFlag = [true, false, false, false];
            $scope.datePickerFlag = [false, false, false, false, false, false];
            $scope.subTabFlag = [true, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.orderPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 200,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
      


            $scope.orderCondition = {
                status: '1'
            };
            $scope.dataList = [];
            $scope.orderDataList = [];
   
  
            $scope.detailInfo = {
                sex: 1,
                locked: 0,
                deliveryReceiptVO: {},
                billList: [],
                basicAttachmentList: []
            };
      
            $scope.tabFlag = {};
        
            $scope.tableConfig = {
                scrollWidth: 1500,
                fixedWidth: 40,
                tableCol: [
                    {
                        type: "",
                        name: "发运交接单号",
                        dataField: "deliveryReceiptNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        // clickFn: "viewDetail"
                    },
                    {
                        type: "",
                        name: "始发地",
                        dataField: "beginningPlace",
                        width: 250,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "发货时间",
                        dataField: "deliveryTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "重量",
                        dataField: "weight",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "尺寸",
                        dataField: "measurement",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "体积",
                        dataField: "volume",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "到货时间",
                        dataField: "arrivalTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "标准时效",
                        dataField: "standardTimeframe",
                        width: 130,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "收货方",
                        dataField: "recipient",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "收货地址",
                        dataField: "shippingAddress",
                        width: 250,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "联系人",
                        dataField: "contacts",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "费用",
                        dataField: "cost",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "运输方式",
                        dataField: "modeOfTransport",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "签收单是否上传",
                        dataField: "uploadDeliveryReceipt",
                        width: 130,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "image",
                        name: "签收单照片",
                        dataField: "deliveryReceiptPicture",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "同步时间",
                        dataField: "syncTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "承运商名称",
                        dataField: "carrierOrgName",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                ]
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    },
                    scrollLeftWidth: {
                        width: $scope.tableConfig.fixedWidth + "px"
                    },
                };
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/wantaiWaybillReport/export"),
                allUrl: ybHttp.RequestUrl("device", "/wantaiWaybillReport/exportAll"),
                sectionParam: {
                    ids: []
                },
            };


            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        $scope.headFlagTwo = "";
                        $scope.headFlagThird = "";
                        initData();
                        break;
                    case 1:
                        $scope.headFlagTwo = "";
                        $scope.headFlagThird = "";
                        break;
                    case 2:
                        $scope.headFlagTwo = "detail";
                        break;
                    default:
                        $scope.headFlagTwo = "";
                        $scope.headFlagThird = "";
                        break;
                }
            };
            function initData() {
                $scope.sortObj = {};

                $scope.tabFlag = {};
                $scope.detailInfo = {
                    sex: 1,
                    locked: 0,
                    deliveryReceiptVO: {},
                    billList: [],
                    basicAttachmentList: []
                };
            }
     
            $scope.packageMaterialList = {
                barcodeList:[],
            };

            function getDataList(curPage) {
                $scope.exportParam.sectionParam.billIds = [];
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/wantaiWaybillReport/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.deliveryTimeShow = $filter("YbDate")(item.deliveryTime, 'yyyy-MM-dd HH:mm:ss')
                        item.arrivalTimeShow = $filter("YbDate")(item.arrivalTime, 'yyyy-MM-dd HH:mm:ss')
                        item.syncTimeShow = $filter("YbDate")(item.syncTime, 'yyyy-MM-dd HH:mm:ss')
                        item.isChecked = false;
                        if (item.deliveryReceiptPicture) {
                          // item.deliveryReceiptPictureList = item.deliveryReceiptPicture.split(',').map(imageUrl => getImgUrlShow(imageUrl))
                          item.deliveryReceiptPictureList = item.deliveryReceiptPicture.split(',');
                      }
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.sortObj = {};
            $scope.toSort = function (config, index) {
                if (!config[index].toSort) {
                    return;
                }
                $scope.sortObj.sortMethod = ybWidget.ToSort(config, index);
                $scope.sortObj.sortClumn = config[index].dataField;
                getBillList(1);
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false, false, false, false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };
            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };


            $scope.reset = function () {
                const wjPriceExprotFlagTemp = $scope.condition.wjPriceExprotFlag;
                $scope.condition = {
                    wjPriceExprotFlag: wjPriceExprotFlagTemp
                };
            };

            $scope.selectForAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                getBillIds();
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
                getBillIds();
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.billIds = [];
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        // $scope.exportParam.sectionParam.billIds.push(item.id);
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function getBillList(curPage) {
                var service = "device";
                var reqUrl = "/deliveryReceipt/outBillList";
                if ($scope.tabFlag.isThreeDayPlan) {
                    reqUrl = "/deliveryReceipt/threeDayPlanBillList";
                }
                var param = {};
                param.pageSize = $scope.orderPageInfo.pageSize;
                if (!!curPage) {
                    $scope.orderPageInfo.curPage = curPage;
                }
                param.pageNum = $scope.orderPageInfo.curPage;
                param.filters = {
                    billType: $scope.orderCondition.billType,
                    warehouseCode: $scope.orderCondition.warehouseCode,
                    planDate: $scope.orderCondition.planDate,
                    keyWords: $scope.orderCondition.keWords,
                    sortClumn: $scope.sortObj.sortClumn,
                    sortMethod: $scope.sortObj.sortMethod,
                    queryType: 1
                };
                $scope.orderDataList = [];
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }

                    if (!resp.value) {
                        resp.value = { list: [] }
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateStr = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.orderPageInfo.totalRows = resp.value.total;
                    $scope.orderPageInfo.totalPages = resp.value.pages;
                    $scope.orderDataList = resp.value.list;
                });
            }

            function viewDetail(item) {
                var service = "device";
                var reqUrl = "/wantaiWaybillReport/item";
                var param = {};
                param.id = item.id;
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                  console.log("resp:",resp);
                  
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.changeTab(2);
                    $scope.detailInfo = resp.value;
                });
            }

            $scope.clickValue = false;
            $scope.saveDataflag = 0;
       
            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };
            function IsPC() {
                var userAgentInfo = navigator.userAgent.toLowerCase();
                var Agents = ["android", "iphone", "symbianos", "windows phone", "ipad", "ipod"];
                var flag = true;
                for (var v = 0; v < Agents.length; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                        flag = false;
                        break;
                    }
                }
                return flag;
            };
            $scope.isPC = false;

            $scope.viewImage = function (item) {
                ybWidget.ViewImage({
                    FileUrl: item
                })
            };

            function getImgUrlShow(val) {
                // console.log("getImgUrlShow val:",val);
                // let res = ybConfig.getConfig().baseUrl.BjxlshFtp + val;
                // console.log("getImgUrlShow res:",res);
                // return res;
                return val;
              
                // return ybConfig.getConfig().baseUrl.BjxlshFtp + val;
            }

            (function () {
                $scope.isPC = IsPC();
                $scope.setFixedWidth();
                // getDataList()
            })();
        }]);
})(window, window.angular);
